package com.cxh.dao.impl;

import com.common.dao.BaseDao;
import com.cxh.dao.A_goodsDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.common.entity.A_goods;

public class A_goodsDaoImpl extends BaseDao<A_goods> implements A_goodsDao {
	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer goods_id,	String goods_name,	 Integer goodsType_id,
	   String describe,		Double buyoutPrice,  Double minPrice,
	   Integer customer_id, String picImg,		 Integer examine,
	   Integer assetLv_id
	 */
	public List<A_goods> findByCondition(Map<String, Object> mp) {
		List<Object> pList = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" select * from a_goods where 1=1 ");
		if(!(mp==null||mp.size()<1))
			for(Entry<String, Object> et:mp.entrySet()){
				if("goods_name".equals(et.getKey())){
					sb.append("and "+et.getKey()+" like ?");
					pList.add("%"+et.getValue()+"%");
				}
				else{
					sb.append("and "+et.getKey()+" = ? ");
					pList.add(et.getValue());
				}
			}
		return findBySql(sb.toString(), pList.toArray());
	}

	@Override
	public A_goods findById(int id) {
		String sql = " select * from a_goods where goods_id = ? ";
		Object[] obj = {id};
		List<A_goods> lg = findBySql(sql, obj);
		return (lg!=null&&lg.size()>0)?lg.get(0):null;
	}

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer goods_id,	String goods_name,	 Integer goodsType_id,
	   String describe,		Double buyoutPrice,  Double minPrice,
	   Integer customer_id, String picImg,		 Integer examine,
	   Integer assetLv_id
	 */
	public int updateA_goods(Map<String,Object> mp) {
		String sql = "update A_goods set goods_name=?, goodsType_id=?, describe=?, buyoutPrice=?,"+
				" minPrice=?, customer_id=?, picImg=?, examine=?,	assetLv_id=? where goods_id=?";
		Object[] objs = {mp.get("goods_name"),mp.get("goodsType_id"),mp.get("describe"),mp.get("buyoutPrice"),
				mp.get("minPrice"),mp.get("customer_id"),mp.get("picImg"),mp.get("examine"),
				mp.get("assetLv_id"),mp.get("goods_id")};
		return executeUpdate(sql, objs);
	}

	@Override
	public int deleteA_goods(int id) {
		String sql = "delete from A_goods where goods_id = ?";
		Object[] objs = {id};
		return executeUpdate(sql, objs);
	}

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer goods_id,	String goods_name,	 Integer goodsType_id,
	   String describe,		Double buyoutPrice,  Double minPrice,
	   Integer customer_id, String picImg,		 Integer examine,
	   Integer assetLv_id
	 */
	public int addA_goods(Map<String,Object> mp) {
		String sql = "insert into A_goods values(SEQ_A_GOODS.nextval,?,?,?,?,?,?,?,?,?)";
		Object[] objs = {mp.get("goods_name"),mp.get("goodsType_id"),mp.get("describe"),mp.get("buyoutPrice"),
				mp.get("minPrice"),mp.get("customer_id"),mp.get("picImg"),mp.get("examine"),
				mp.get("assetLv_id")};
		return executeUpdate(sql, objs);
	}
	
	public A_goods getEntity(ResultSet rs){
		try {
			A_goods g = new A_goods();
			g.setAssetLv_id(rs.getInt("assetLv_id"));
			g.setBuyoutPrice(rs.getDouble("buyoutPrice"));
			g.setCustomer_id(rs.getInt("customer_id"));
			g.setDescribe(rs.getString("describe"));
			g.setExamine(rs.getInt("examine"));
			g.setGoods_id(rs.getInt("goods_id"));
			g.setGoods_name(rs.getString("goods_name"));
			g.setGoodsType_id(rs.getInt("goodsType_id"));
			g.setMinPrice(rs.getDouble("minPrice"));
			g.setPicImg(rs.getString("picImg"));
			return g;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int countGoods() {
		String sql = "A_goods";
		return getCount(sql, null);
	}

	@Override
	public int countGoodsType(int id) {
		String countSql=" select count(*) c from A_goods where goodsType_id = ?";
		Object[] objs = {id};
		Connection conn=this.getConnection();
		PreparedStatement prs=null;
		ResultSet rs=null;
		try {
			prs=conn.prepareStatement(countSql);
			if(objs!=null && objs.length>0){
				for(int i=0;i<objs.length;i++){
					prs.setObject(i+1, objs[i]);
				}
			}
			//3.3銆佹墽琛屾煡璇�
			rs=prs.executeQuery();
			if(rs.next()){
				return rs.getInt("c");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			this.closeAll(conn, prs, rs);
		}
		return 0;
	}
}
